package com.wangdong.mijiqiqi.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.wangdong.mijiqiqi.R;

import java.lang.reflect.Method;

public class BluetoothActivity extends AppCompatActivity {
    //创建蓝牙适配器
    private BluetoothAdapter mBluetoothAdapter = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //本地蓝牙适配器  是为所有蓝牙交互的入口点，//获取默认BluetoothAdapter，实际上，也只有这一种方法获取BluetoothAdapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        // 搜索设备
        //首先动态注册广播
        IntentFilter filter = new IntentFilter();
        filter.setPriority(Integer.MAX_VALUE);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(receiver, filter);
    }

    //创建广播接收器
    public BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device;
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                String address = device.getAddress();
                int state = device.getBondState();
//                WDLogUtils.d(String.format("found bluetooth device name=%s address=%s state=%s", name, address, state == BluetoothDevice.BOND_BONDED ? "BOND_BONDED" : "BOND_NONE"));
//                if (!adapter.getDevices().contains(device)) {
//                    adapter.addDevice(device);
//                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//                LogUtils.d("select bluetooth device over!!!");
                if (mBluetoothAdapter.isDiscovering()) {
                    mBluetoothAdapter.cancelDiscovery();
                }
            } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                switch (device.getBondState()) {
                    case BluetoothDevice.BOND_BONDING:
//                        WDL.d("bluetooth device bonding....");
                        break;
                    case BluetoothDevice.BOND_BONDED:
//                        adapter.notifyDataSetChanged();
//                        LogUtils.d("bluetooth device bonded");
                        break;
                    case BluetoothDevice.BOND_NONE:
//                        LogUtils.d("bluetooth device none!!!");
                        break;
                    default:
                        break;
                }
            }
        }
    };

    //配对方法
    public boolean createBond(BluetoothDevice device) {
        try {
            Method createBondMethod = device.getClass().getMethod("createBond");
            return (Boolean) createBondMethod.invoke(device);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
